package com.dckj.auto.controller;


import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dckj.auto.common.interceptor.AuthorityAnnotation;
import com.dckj.auto.common.utils.AuthCode;
import com.dckj.auto.common.utils.JsonUtils;
import com.dckj.auto.common.utils.ehcache.EHCacheUtils;
import com.dckj.auto.controller.help.UserMenuHelp;
import com.dckj.auto.entity.AuthMenu;
import com.dckj.auto.entity.AuthMenuRole;
import com.dckj.auto.service.AuthMenuRoleService;
import com.dckj.auto.service.AuthMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangDong123
 * @since 2018-09-03
 */
@Controller
@RequestMapping("/menuRole/")
public class AuthMenuRoleController {
    Props prop = new Props("config.properties","UTF-8");
    private  String authTokenCacheKey = prop.getStr("auth_token_cache_key");

    @Autowired
    AuthMenuService menuService;
    @Autowired
    AuthMenuRoleService menuRoleService;

    @AuthorityAnnotation(authority = AuthCode.MENU_ROLE_INNER)
    @RequestMapping(value = "inner")
    public String inner(HttpServletRequest request, String roleId,Integer type){
        request.setAttribute("roleId",roleId);
        request.setAttribute("type",type);
        AuthMenu menu = menuService.selectOne(new EntityWrapper<AuthMenu>().eq("attribute", null == type ? 1 : type).eq("parent_id", -1));
        if(null == menu){
            return "menu-role/inner";
        }
        request.setAttribute("title",menu.getName());
        request.setAttribute("bossId",menu.getMenuId());
        request.setAttribute("attribute",menu.getAttribute());
        return "menu-role/inner";
    }

    @RequestMapping(value = "getAuto")
    @ResponseBody
    public JSONObject getAuto(HttpServletRequest request,String roleId,Integer type){
        JSONArray jsonArray = UserMenuHelp.userAuthMenu(request,1);//获取当前用户的权限
        List<AuthMenu> list = menuService.list(null == type ? 1 : type);
        //查询角色关联过的
        List<AuthMenuRole> menuRoles = menuRoleService.selectList(new EntityWrapper<AuthMenuRole>().eq("role_id", roleId));
        List<AuthMenu> list2 = new ArrayList<>();
        if(null != jsonArray){//当前用户没有的权限也不能分配给其他用户，只能分配用用户自己拥有的权限
            String s = jsonArray.toString();
            for (AuthMenu menu : list){
                if(s.contains(menu.getName())){
                    list2.add(menu);
                }
            }
        }
        for (AuthMenu menu : list2){
            String menuId = menu.getMenuId();
            menu.setChecked(2);//都是默认不选中
            if(menu.getParentId().equals(-1)){
                menu.setChecked(1);
            }
            //循环角色关联的
            if(menuRoles.size()>0){
                for (AuthMenuRole menuRole :  menuRoles){
                    String menuId1 = menuRole.getMenuId();
                    if(menuId1.equals(menuId)){//选了
                        menu.setChecked(1);
                        break;
                    }
                }
            }
        }
        JSONObject o = new JSONObject();
        o.put("auto",list2);
        return JsonUtils.success(o);
    }

    @RequestMapping(value = "doInner")
    @ResponseBody
    public JSONObject doInner(String[] menuIds,String roleId,Integer attribute){
        Integer inner = menuRoleService.insert(menuIds, roleId,attribute);
        if(inner>0){
            EHCacheUtils cache = EHCacheUtils.getInstance();
            cache.remove(authTokenCacheKey);//清除用户权限缓存
            return JsonUtils.success("编辑角色关联菜单");
        }
        return JsonUtils.error("编辑角色关联菜单");
    }
}

